package search.model.query;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.*;

import search.config.SearchParameters;
import common.helper.serialization.MyDateDeserializer;
import common.helper.serialization.MyDateSerializer;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

@Entity
public class ComplexParameter {

    public ComplexParameter(){
    }

	//*************************//
	/*
	 * from
	*/
	public Integer from = 0;
	public Integer getFrom(){return from;}
	public void setFrom(Integer from) {this.from = from;}	
	
	/*
	* size
	*/
	private Integer size = 10;
	public Integer getSize(){return size;}
	public void setSize(Integer size) {this.size = size;}

	//*************************//
    /*
    * 全文搜索字段 fulltext
    * 简单搜索使用该字段
    * 大致对应Elasticsearch中的_all字段
    */
    private String fulltext;
    public String getFulltext(){
        return fulltext;
    }
    public void setFulltext(String fulltext){
        this.fulltext= fulltext;
	}

	/*
	 * pinyin
	 */
	private String pinyin;
	public String getPinyin() {
		return pinyin;
	}
	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

    
    /*
     * title
     */
    @JsonIgnore
    public String getTitle(){
    	return fulltext;
    }
    /*
     * description
     */
    @JsonIgnore
    public String getDescription(){
    	return fulltext;
    }



	//***************************//
	/* 
	 * levels 指定要搜索的编目层次
	 * 用一个json数组传入
	 * 当数组为空,搜索所有节目层次;
	 * 当数组不为空,只搜索数组中指定的层次
	 */
	private List<String> levels = new ArrayList<String>();
	public List<String> getLevels(){
		return levels;
	}
	public void setLevels(List<String> levels){
		this.levels = levels;
	}


	/*
	 * minimum
	 * 文档中至少有 "minimum" 的关键词和查询匹配
	 * minimum 本身是一个表达式，由系统解析。
	 * 查询：所有字段的合并，包括全文、来源、地区等等
	 * 文档：这整个文档的所有字段
	 */
	private String minimum = SearchParameters.MinimumMatch.ori_minimum;  // at least (n-1) word match
	public String getMinimum() { return minimum;}
	public void setMinimum(String percent) { this.minimum = percent;}

	/*
	* explain 是否解释
	 */
	private String explain = "false";
	public void setExplain(String explain){this.explain = explain;}
	public String getExplain(){return this.explain;}

	/*
	* dsl 是否返回dsl
	 */
	private String dsl = "false";
	public void setDsl(String dsl){this.dsl = dsl;}
	public String getDsl(){return this.dsl;}




	//************************//
	/*
	 * 媒体分类
	 * mediatype
	 * 视频 音频 图片
	 */
	private String mediatype = "video";
	public String getMediatype(){return mediatype;}
	public void setMediatype(String mediatype){this.mediatype = mediatype;}
	
	/*
	 * 排序方式
	 * sorttype
	 * 相关度、最早上传、最晚上传等
	 */
	private String sorttype = "relevancy";
	public String getSorttype(){return sorttype;}
	public void setSorttype(String sorttype){this.sorttype = sorttype;}
	
	
	//**************************//
	
	/*
	 * 来源
	 * source
	 * CCTV BTV 等
	 */
	private String source;
	public String getSource(){return source;}
	public void setSource(String source){ this.source = source;}
	
	/*
	 * 类型
	 * mediaform
	 * 成片 素材
	 */
	//@JsonProperty(value = "class")
	private String mediaform;
	public String getMediaform(){return mediaform;}
	public void setMediaform(String mediaform){this.mediaform = mediaform;}
	
	/*
	 * 内容分类
	 * subject
	 * 创意 自然 动画 特效
	 */
	private String subject;
    public String getSubject(){return subject;}
    public void setSubject(String subject){this.subject = subject;}
	
	/*
	 * 地区
	 * region
	 * 中国 美国
	 */
    private String region;
    public String getRegion(){return region;}
    public void setRegion(String region) {this.region = region;}
    
    /*
     * 尺寸
     * mediasize
     * web HD 4K
     */
    private String mediasize;
    public String getMediasize(){return mediasize;}
    public void setMediasize(String mediasize){this.mediasize = mediasize;}
    
    /*
     * 格式
     * format
     * mp4 mkv avi
     */
    private String format;
    public String getFormat(){return format;}
    public void setFormat(String format) {this.format = format;}

	/*
	* 版权类型 
	* copyright
	* RM版权素材  RF版权素材
	*/
	private String copyright;
	public String getCopyright(){return copyright;}
	public void setCopyright(String copyright) {this.copyright = copyright;}

	//***********************************//
	/*
	* 拍摄时间（起） 
	* shotdatefrom
	* yyyy-MM-dd HH:mm:ss
	*/
	private Date shotdatefrom;
	@JsonSerialize(using = MyDateSerializer.class)
	public Date getShotdatefrom(){return shotdatefrom;}
	@JsonDeserialize(using = MyDateDeserializer.class)
	public void setShotdatefrom(Date shotdatefrom) { this.shotdatefrom = shotdatefrom;}

	/*
	* 拍摄时间（止） 
	* shotdateto 
	* yyyy-MM-dd HH:mm:ss
	 */
	private Date shotdateto;
	@JsonSerialize(using = MyDateSerializer.class)
	public Date getShotdateto(){return shotdateto;}
	@JsonDeserialize(using = MyDateDeserializer.class)
	public void setShotdateto(Date shotdateto) { this.shotdateto = shotdateto;}


	//*************************************//
	/*
	 * getAllFiled
	 */
	@JsonIgnore
	public String getAllField(){
		StringBuffer buffer = new StringBuffer();
		if(getFulltext() != null && getFulltext() != "")
			buffer.append(getFulltext()+" ");
		if(getSource() != null && getSource() != "")
			buffer.append(getSource()+ " ");
		if(getMediaform() != null && getMediaform() != "")
			buffer.append(getMediaform()+ " ");
		if(getSubject() != null && getSubject() != "")
			buffer.append(getSubject()+ " ");
		if(getRegion() != null && getRegion() != "")
			buffer.append(getRegion()+ " ");
		if(getMediasize() != null && getMediasize() != "")
			buffer.append(getMediasize()+ " ");
		if(getFormat() != null && getFormat() != "")
			buffer.append(getFormat());
		if(getCopyright() != null && getCopyright() != "")
			buffer.append(getCopyright()+ " ");
		return buffer.toString();
	}
	
	/*
	 * getParamCount
	 */
	@JsonIgnore
	public Integer getParamCount(){
		Integer Count = 0;
		if(getFulltext() != null && getFulltext() != "")
			Count ++;
		if(getSource() != null && getSource() != "")
			Count ++;
		if(getMediaform() != null && getMediaform() != "")
			Count ++;
		if(getSubject() != null && getSubject() != "")
			Count ++;
		if(getRegion() != null && getRegion() != "")
			Count ++;
		if(getMediasize() != null && getMediasize() != "")
			Count ++;
		if(getFormat() != null && getFormat() != "")
			Count ++;
		if(getCopyright() != null && getCopyright() != "")
			Count ++;
		return Count;
	}
	//*************************************//
	/*
	* to Josn String
	*/
    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
		String json = "";
		try {
			json = mapper.writeValueAsString(this);
		} catch (IOException e){
			e.printStackTrace();
		}
		return json;
    }
}
